import { ref } from "vue";
/**
 * 发送短信验证码
 * @param countDown
 * @param text
 * @param resendText
 * @returns
 */
export function useSendCode(
  countDown = 60,
  text = "获取验证码",
  resendText = "重新获取"
) {
  /**
   * 管理员手机号
   */
  const adminPhone = "17776666611";
  /**
   * 发送验证码逻辑
   */
  // 是否发送
  const isSend = ref(false);
  const codeText = ref(text);
  function onSendCode() {
    if (isSend.value) return;
    isSend.value = true;
    let n = countDown;
    codeText.value = `${n}s`;
    const run = setInterval(() => {
      n = n - 1;
      if (n < 0) clearInterval(run);

      codeText.value = `${n}s`;
      if (Number.parseInt(codeText.value) < 0) {
        isSend.value = false;
        codeText.value = resendText;
      }
    }, 1000);
  }
  return {
    adminPhone,
    isSend,
    codeText,
    onSendCode
  };
}
